#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import rospy
import json,os
from flask import Blueprint,request,Response

from tool.common import *
from parser.map_parser import MapParser

map_parser = MapParser()
map_server = Blueprint("/map",__name__)

#---------------------------------#
#******#   flask-网络端口验证   #******#
#---------------------------------#
@map_server.before_request
def before():
    # print("map_server")
    res_data = before_user(request)
    if res_data:
        return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
# 地图基础操作
# ft.wang 20240606接手
#---------------------------------#
# 开始地图新建
@map_server.route('/draw_new_map',methods=['POST'])
def Post_CreateMap():
    res_data = map_parser.CreateMap(True)
    return ResponseDataForJson(res_data)

# 取消地图新建
@map_server.route('/cancel_map_builiding',methods=['POST'])
def Post_CancelCreateMap():
    res_data = map_parser.CreateMap(False)
    return ResponseDataForJson(res_data)

# 地图保存
@map_server.route('/map_save',methods=['POST'])
def Post_SaveMap():
    res_data = map_parser.SaveMap(request.json)
    return ResponseDataForJson(res_data)

# 获取当前地图名称
@map_server.route('/map_name',methods=['GET', 'POST'])
def Post_GetMapName():
    res_data = map_parser.GetMapName()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取地图列表
@map_server.route('/map_list',methods=['GET'])
def Post_GetMapList():
    res_data = map_parser.GetMapList()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 地图信息查询
@map_server.route('/map_info',methods=['POST'])
def Post_GetMapInfo():
    data = request.json
    res_data = map_parser.GetMapInfo(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 地图重命名
@map_server.route('/map_rename',methods=['POST'])
def Post_RenameMap():
    res_data = map_parser.RenameMap(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 删除地图
@map_server.route('/map_del',methods=['POST'])
def Post_DeleteMap():
    res_data = map_parser.DeleteMap(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 还原地图
@map_server.route('/map_renew',methods=['POST'])
def Post_RenewMap():
    res_data = map_parser.ResetMap(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 复制地图
@map_server.route('/map_copy',methods=['POST'])
def Post_MapCopy():
    res_data = map_parser.CopyMap(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 清除地图杂点
@map_server.route('/map_clear_noise',methods=['POST'])
def Post_MapClearNoise():
    res_data = map_parser.MapClearNoise(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 地图加载
@map_server.route('/map_load',methods=['POST'])
def Post_LoadMap():
    res_data = map_parser.LoadMap(request.json)
    return ResponseDataForJson(res_data)

# 开始地图更新
@map_server.route('/map_update_start',methods=['POST'])
def Post_StartMapUpdate():
    res_data = map_parser.UpdateMap(True)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 取消地图更新
@map_server.route('/map_update_cancel',methods=['POST'])
def Post_CancelMapUpdate():
    res_data = map_parser.UpdateMap(False)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 地图更新保存
@map_server.route('/map_update_save',methods=['POST'])
def Post_SaveMapUpdate():
    res_data = map_parser.SaveUpdatedMap(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
# 标记点操作
# ft.wang 20240606接手
#---------------------------------#
# 添加标记点
@map_server.route('/map_flag_add',methods=['POST'])
def Post_AddMarkerPoint():
    res_data = map_parser.AddMarkerPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 删除标记点
@map_server.route('/map_flag_del',methods=['POST'])
def Post_DeleteMarkerPoint():
    res_data = map_parser.DeleteMarkerPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 修改标记点
@map_server.route('/map_flag_update',methods=['POST'])
def Post_UpdateMarkerPoint():
    res_data = map_parser.UpdateMarkerPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取标记点
@map_server.route('/map_flag_list',methods=['POST'])
def Post_ListMarkerPoint():
    res_data = map_parser.MarkerPointList(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
# 虚拟数据操作
# ft.wang 20240606接手
#---------------------------------#
# 虚拟轨道信息查询
@map_server.route('/virlines_info',methods=['POST'])
def Post_GetVirtualLines():
    res_data = map_parser.GetVirtualLines(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 编辑地图-栅格图 虚拟墙
@map_server.route('/map/Upload',methods=['POST'])
def Post_UpdateVirtualLines():
    # print("json: ", request.json)
    # print("form: ", request.form)
    # print("args: ", request.args)
    # print(request.files)
    res_data = map_parser.ResDataInit()
    data = request.form
    if "map_name" in data:
        map_name = data["map_name"].encode('utf-8')
    
        png_str = ""
        if request.files.get('file') is not None:
            png_str = request.files['file'].read()
            # print("file: ", request.files['file'])
            # # print("file: ", request.files['file'].read())
            # map_dir = rospy.get_param("/map_dir", "/home/aoting/user_config/map_database/")
            # map_png_file = os.path.join(map_dir, map_name, "map_edit.png")
            # request.files['file'].save(map_png_file)
        if "virlines_wall" in data:
            res_data = map_parser.UpdateVirtualLines(data, png_str)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 添加功能区 ???
@map_server.route('/ribbon_list',methods=['POST'])
def Post_RibbonList():
    res_data = map_parser.RibbonList(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 功能区更新
@map_server.route('/ribbon_edit',methods=['POST'])
def Post_RibbonEdit():
    res_data = map_parser.RibbonEdit(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
# 电梯位姿操作
# ft.wang 20240606接手
#---------------------------------#

# 添加电梯标记点
@map_server.route('/elevator_point_add',methods=['POST'])
def Post_AddElevatorMarkerPoint():
    res_data = map_parser.AddElevatorMarkerPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 删除电梯标记点
@map_server.route('/elevator_point_del',methods=['POST'])
def Post_DeleteElevatorMarkerPoint():
    res_data = map_parser.DeleteElevatorMarkerPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 修改电梯标记点
@map_server.route('/elevator_point_update',methods=['POST'])
def Post_UpdateElevatorMarkerPoint():
    res_data = map_parser.UpdateElevatorMarkerPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取电梯标记点列表
@map_server.route('/elevator_point_list',methods=['POST'])
def Post_ListElevatorMarkerPoint():
    res_data = map_parser.ElevatorMarkerPointList(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取机器所有地图的充电桩标记点列表
@map_server.route('/robot_maps_flag_list',methods=['POST'])
def Post_RobotMapsFlagList():
    res_data = map_parser.RobotMapsFlagList()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 设置机器人梯控充电桩位置
@map_server.route('/elevator_docker_set',methods=['POST'])
def Post_ElevatorDockerSet():
    res_data = map_parser.ElevatorDockerSet(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 切换地图楼层
@map_server.route('/go_elevator_map',methods=['POST'])
def Post_GoElevtorMap():
    res_data = map_parser.GoElevtorMap(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')


# 地图信息设置, 主要指旋转角度
@map_server.route('/map_info_set',methods=['POST'])
def Post_MapInfoSet():
    res_data = map_parser.MapInfoSet(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 获取所有的地图及地图下所有任务
@map_server.route('/task_all_map',methods=['POST'])
def Post_ReportMapTaskList():
    res_data = map_parser.AllMapTaskList()
    return Response(json.dumps(res_data),  mimetype='application/json')

#---------------------------------#
# 重定位操作
#---------------------------------#

# 区域选点重定位 从robot_server迁移至此
@map_server.route('/relocation_point',methods=['POST'])
def Post_AreaRelocation():
    res_data = map_parser.AreaRelocation(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 标记点重定位 从robot_server迁移至此
@map_server.route('/relocation_start',methods=['POST'])
def RelocationPoint():
    res_data = map_parser.RelocationPoint(request.json)
    return Response(json.dumps(res_data),  mimetype='application/json')


# 反光板重定位
@map_server.route('/relocation_barcode',methods=['POST'])
def RelocationBarCode():
    res_data = map_parser.RelocationBarCode()
    return Response(json.dumps(res_data),  mimetype='application/json')


#---------------------------------#
# 门禁闸机操作
#---------------------------------#

# 门禁标记添加_开始
@map_server.route('/door_access_control_start',methods=['POST'])
def Post_DoorAccessControlStart():
    print("---------Post_DoorAccessControlStart---------")
    res_data = map_parser.DoorAccessControlStart()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 门禁标记添加_门禁点
@map_server.route('/door_access_control_center',methods=['POST'])
def Post_DoorAccessControlCenter():
    res_data = map_parser.DoorAccessControlCenter()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 门禁标记添加_结束 保存
@map_server.route('/door_access_control_end',methods=['POST'])
def Post_DoorAccessControlEnd():
    data = request.json
    res_data = map_parser.DoorAccessControlEnd(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 门禁标记添加_取消 
@map_server.route('/door_access_control_cancel',methods=['POST'])
def Post_DoorAccessControlCancel():
    res_data = map_parser.DoorAccessControlCancel()
    return Response(json.dumps(res_data),  mimetype='application/json')

# 门禁标记列表 
@map_server.route('/door_access_control_list',methods=['POST'])
def Post_DoorAccessControlList():
    data = request.json
    res_data = map_parser.DoorAccessControlList(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 门禁标记删除 
@map_server.route('/door_access_control_del',methods=['POST'])
def Post_DoorAccessControlDel():
    data = request.json
    res_data = map_parser.DoorAccessControlDel(data)
    return Response(json.dumps(res_data),  mimetype='application/json')

# 门禁标记修改 
@map_server.route('/door_access_control_edit',methods=['POST'])
def Post_DoorAccessControlEdit():
    data = request.json
    res_data = map_parser.DoorAccessControlEdit(data)
    return Response(json.dumps(res_data),  mimetype='application/json')